package com.ruoyi.web.controller.douyin;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.model.LoginUser;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.DyGoodsVo;
import com.ruoyi.system.domain.bo.DyGoodsBo;
import com.ruoyi.system.service.IDyGoodsService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品
 *
 * @author ruoyi
 * @date 2024-06-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/goods")
public class DyGoodsController extends BaseController {

    private final IDyGoodsService iDyGoodsService;

    /**
     * 查询商品列表
     */
    @SaCheckPermission("system:goods:list")
    @GetMapping("/list")
    public TableDataInfo<DyGoodsVo> list(DyGoodsBo bo, PageQuery pageQuery) {
        LoginUser loginUser = getLoginUser();
        return iDyGoodsService.queryPageList(bo, pageQuery,loginUser);
    }

    /**
     * 获取商品详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:goods:query")
    @GetMapping("/{id}")
    public R<DyGoodsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iDyGoodsService.queryById(id));
    }

    /**
     * 新增商品
     */
    @SaCheckPermission("system:goods:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DyGoodsBo bo) {
        return toAjax(iDyGoodsService.insertByBo(bo));
    }

    /**
     * 修改商品
     */
    @SaCheckPermission("system:goods:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DyGoodsBo bo) {
        return toAjax(iDyGoodsService.updateByBo(bo));
    }

    /**
     * 删除商品
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:goods:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iDyGoodsService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 导出商品列表
     */
    @SaCheckPermission("system:goods:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DyGoodsBo bo, HttpServletResponse response) {
        List<DyGoodsVo> list = iDyGoodsService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品", DyGoodsVo.class, response);
    }
}
